<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f0f4f8;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }
        .card {
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            padding: 2rem;
            width: 100%;
            max-width: 400px;
        }
        h1 {
            color: #333;
            text-align: center;
            margin-bottom: 0.5rem;
        }
        p {
            color: #666;
            text-align: center;
            margin-bottom: 1.5rem;
        }
        form {
            display: flex;
            flex-direction: column;
        }
        input {
            padding: 0.75rem;
            margin-bottom: 1rem;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 1rem;
        }
        button {
            background-color: #4a90e2;
            color: white;
            padding: 0.75rem;
            border: none;
            border-radius: 4px;
            font-size: 1rem;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        button:hover {
            background-color: #3a7bd5;
        }
        .register-link {
            text-align: center;
            margin-top: 1rem;
            font-size: 0.9rem;
        }
        a {
            color: #4a90e2;
            text-decoration: none;
        }
        a:hover {
            text-decoration: underline;
        }
        .error {
            color: red;
            text-align: center;
            margin-top: 1rem;
            display: none; /* 默认隐藏错误信息 */
        }
    </style>
</head>
<body>
<div class="card">
    <h1>登录</h1>
    <p>输入您的账号信息以登录</p>
    <form id="loginForm" method="post">
        <input type="text" name="username" placeholder="用户名" required>
        <input type="password" name="password" placeholder="密码" required>
        <button type="submit">登录</button>
    </form>
    <div class="register-link">
        还没有账号? <a href="register.html">注册</a>
    </div>
    <div class="error" id="errorMessage"></div>
</div>
<script>
    document.getElementById('loginForm').onsubmit = async function(event) {
        event.preventDefault();
        const formData = new URLSearchParams(new FormData(this)).toString(); // URL 编码表单数据
        const response = await fetch('/user/login', {
            method: 'POST',
            body: formData,
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded', // 设置请求头
                'Accept': 'application/json'
            }
        });

        if (response.ok) {
            window.location.href = '/dashboard.html'; // 登录成功后重定向
        } else {
            const errorMessage = await response.json();
            document.getElementById('errorMessage').innerText = errorMessage.error;
            document.getElementById('errorMessage').style.display = 'block'; // 显示错误信息
        }
    };
</script>
</body>
</html>
